import { environment } from 'environments/environment';
import { store } from 'spd-store';
import { POST, Auth, QueryParams, ResponeList } from 'spd-request';
import { useLocalStore } from 'mobx-react';
import { useState, useEffect } from 'react';
export interface IQueryOrganizationUnitsUsersStruct {
    addedTime: string;
    addedUniversalTime: number;
    assignedRoleDisplayNames: string[];
    assignedRoleNames: [string]
    avatar: null
    creationTime: string;
    displayName: string;
    emailAddress: null
    gender: number;
    id: number;
    isActive: boolean;
    name: string;
    organizationId: number;
    phoneNumber: string;
    position: null
    profilePictureId: null
    surname: string;
    userId: number;
    userName: string;
    userPrimaryId: string;
}
export interface IParams {
    id?: number;
    idList?: number[];
    filter?: string;
    page?: number;
    pageSize?: number;
}
interface Idata {
    totalCount: number, items: IQueryOrganizationUnitsUsersStruct[]
}

export function useUserList(): [Idata, (params?: IParams) => void, boolean] {
    let [users, setUsers] = useState<Idata>({totalCount: 0, items: []});
    let [isLoading, setIsloading] = useState<boolean>(false);
    let [requestParams, setRequestParams] = useState({idList: []});
    function change(params: IParams = {idList: []}) {
        setIsloading(true);
        let newParams = Object.assign(requestParams, {id: 1, filter: '', page: 1, pageSize: 10}, params);
        setRequestParams(newParams);
        POST<ResponeList<IQueryOrganizationUnitsUsersStruct & {idx: number}>>('/OrganizationUnit/QueryOrganizationUnitsUsers', Object.assign(newParams, {maxResultCount: newParams.pageSize, skipCount: newParams.page - 1})).then(res => {
            if(res.success == true) {
                let {totalCount, items} = res.result?res.result: {totalCount: 0, items: []}
                let list = items;
                let i = 1;
                list.forEach(item => {
                    return Object.assign(item, {idx: i++})
                });
                setUsers({totalCount: totalCount, items: list});
            } else {
                setUsers({totalCount: 0, items: []});
            }
        }).finally(() => {
            setIsloading(false);
        }).catch(() => {
            setUsers({totalCount: 0, items: []});
            setIsloading(false);
        })
    }
    useEffect(() => {
        change();
    },[]);
    return [users,change, isLoading];
}